package com.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.dto.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    
    /**
     * 游标分页查询
     * @param lastId 上一页最后一个记录的ID，第一页传null或0
     * @param pageSize 每页大小
     * @return 分页结果
     */
    public PageResult<User> findPageByCursor(Long lastId, Integer pageSize) {
        if (lastId == null) {
            lastId = 0L;
        }
        
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        
        // 限制单页最多1000条记录
        if (pageSize > 1000) {
            pageSize = 1000;
        }
        
        List<User> users = this.baseMapper.selectPageByCursor(lastId, pageSize);
        Long total = this.baseMapper.countByCursor(lastId);
        
        return PageResult.of(users, total, pageSize);
    }
}